#ifndef __CONFIG_SERVER_H__
#define __CONFIG_SERVER_H__


#include <map>
#include <mutex>
#include <functional>
#include "Util/util.h"
#include "json.h"
#include "Network/Buffer.h"


class ConfigServer //: public Fdt::make_shared_base<Config>
{
public:
	using NotifyFunction = std::function<void (const Json::Value &request)>;
	using ConfigFunc = std::function<bool(Json::Value &, int &result)>;
	static ConfigServer *instance()
	{
		if(s_configServer == nullptr)
		{
			s_configServer = new ConfigServer();
		}
		return s_configServer;
	}

	~ConfigServer(){}

	void RegistRPC();

	bool Set(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

	bool Get(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

	bool SetMulti(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

	bool GetMulti(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

	bool RestoreAll(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

	bool GetDefault(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

	bool Restore(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

	bool RestoreMulti(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

	bool Subscribe(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

//	bool attach(const char *name, void *thiz, const ConfigFunc &func);
//
//	bool detach(const char *name, void *thiz);
//
//	bool attachVerify(const char *name, void *thiz, const ConfigFunc &func);
//
//	bool detachVerify(const char *name, void *thiz);

protected:
	ConfigServer();
private:
	std::string getConfigName(const char* name);
private:
	static ConfigServer *			s_configServer;
	std::recursive_mutex			_mutex;

	std::map<std::string, std::map<void *, ConfigFunc>> _setMap;
	std::map<std::string, std::map<void *, ConfigFunc>> _verifyMap;
};



#endif

